"""
模块概述：评价页面数据处理
创建时间：2021-3-2
更新时间：2021-3-15
"""
from flask import Flask, render_template
from flask import Flask,request,render_template,redirect
from flask import Blueprint,render_template, request,session
import datetime
import pymysql

table2=Blueprint('table2',__name__)

@table2.route('/data')
def data():
    conn = pymysql.connect(host='139.217.107.173', user='root', password='123456', db='project_training', charset='utf8')
    cur = conn.cursor()
    sql = r"SELECT * FROM workerinfo WHERE CName= '"+session.get('user')+r"'"
    print(sql)
    cur.execute(sql)
    u = cur.fetchall()
    conn.close()
    lis=[]
    for i in u:
        lis2=[]
        for i2 in i:
            if i2 is None:
                lis2.append('NULL')
            else:
                lis2.append(i2)
        lis.append(lis2)
    data=''
    for i in lis:
        data=data+'{'+r'"id":'+r'"'+'%d'%i[0]+r'",'+r'"name":'+r'"'+i[1]+r'",'+r'"sex":'+r'"'+i[2]
        data=data+r'",'+r'"tel":'+r'"'+i[5]+r'",'+r'"email":'+r'"'+i[6]+r'",'+r'"major":'+r'"'+i[9]
        data=data+r'",'+r'"school":'+r'"'+i[10]+r'",'+r'"education":'+r'"'+i[11]+r'"'+"},"
    data=data[:-1]
    str=r'{"code":0,"msg":"","count":100,"data":['+data+r']}'
    return str

@table2.route('/query',methods=['GET','POST'])
def query():
    cons=str(dict(request.form))
    cons=cons[3:-6]
    mark=cons[-1]
    mes=''
    com=''
    num=0
    for i in cons:
        if i==']':
            mes=cons[:num]
            com=cons[num+1:-1]
            break
        num = num + 1
    #print(mes+'/'+com+'/'+mark)
    user_dict=eval(mes)
    user_id=user_dict['id']
    ename=session.get('user')


    conn = pymysql.connect(host='139.217.107.173', user='root', password='123456', db='project_training', charset='utf8')
    cur = conn.cursor()
    sql = "SELECT * FROM workerevaluator"
    cur.execute(sql)
    u = cur.fetchall()
    comid='%d'%(len(u)+1)
    comtim=datetime.datetime.now().strftime('%Y-%m-%d')
    sql=r'INSERT INTO workerevaluator VALUES("'+comid+r'","'+user_id+r'","'+com+r'","'+comtim+r'","'+mark+r'","'+ename+r'");'
    print(sql)
    cur.execute(sql)
    conn.commit();
    conn.close()
    #print(session.get('user'))
    return render_template('table2.html')
